package com.lfg.leetcode.leetcode300;


public class No376wiggleMaxLength {
    public static void main(String[] args) {
//        int ret = maxEnvelopes(new int[][]{
//                new int[]{1, 3},
//                new int[]{3, 5},
//                new int[]{6, 7},
//                new int[]{6, 8},
//                new int[]{8, 4},
//                new int[]{9, 5}
//        });
        // 1 17 5 13
        // 1 17 5 15 10,
        //
        int ret = wiggleMaxLength(new int[]{
                1, 17, 5, 10, 13, 15, 10, 5, 16, 8
        });
        System.out.println(ret);

    }

    public static int wiggleMaxLength(int[] nums) {
        if (nums.length < 2) {
            return nums.length;
        }
        int[] up = new int[nums.length]; // 摆动序列，第i个代表上升，i+1期望下降
        int[] down = new int[nums.length]; // 摆动序列，第i个代表下降，i+1期望上升
        up[0] = down[0] = 1;
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] == nums[i - 1]) {
                up[i] = up[i - 1];
                down[i] = down[i - 1];
            } else if (nums[i] > nums[i - 1]) {// 接下来期望下降。
                down[i] = down[i - 1];
                up[i] = Math.max(up[i - 1], down[i - 1]+1);
            } else {
                up[i] = up[i - 1];
                down[i] = Math.max(down[i - 1], up[i - 1] + 1);
            }
        }
        return Math.max(up[nums.length-1], down[nums.length-1]);
    }
}



